\setupinteraction[state=start, focus=standard, color=darkred]
% layout
\setuppapersize[A4][A4]
\setuplayout[backspace=3cm,width=15cm,
             topspace=1.5cm,header=1.5cm,
             height=27.2cm,footer=1cm]
%\showframe
\setuphead[title][header=empty, footer=empty, style=\bfc, align={middle,broad}]
\setuphead[subject,section][style=\bfa]

\setupfootnotes[textstyle=bold]
\setuppagenumbering[location={footer, right}]

\setuphead[subject][incrementnumber=list]
\setupcombinedlist[content][list={section,subject},criterium=text]
\def\PageNumber#1{\underbars{#1}.}
\setuplist
  [section,subject]
  [alternative=c,
   width=2em,
   pagecommand=\PageNumber,
   pagestyle=small]

\setupwhitespace[medium]
\setupinterlinespace[line=16pt]

\def\dollar{\color[darkblue]{\tt \$}}

\startuniqueMPgraphic{box quote}
path p, q;
numeric w, h, u;
u := BodyFontSize;
w := OverlayWidth; h := .85OverlayHeight;
color framecolor; framecolor := white randomized (.7, .7, .7);
pickup pencircle scaled 1pt;
% upper left corner
p := ((0, h) -- (1.5u, h)) shifted (-.1u, -.1u);
q := ((0, h) -- (0, h - .75u)) shifted (0, .1u);
draw p withcolor framecolor;
draw q withcolor framecolor;
% lower right corner
p := ((w - 1.5u, 0) -- (w, 0)) shifted (.1u, 0);
q := ((w, 0) -- (w, .75u)) shifted (0, -.1u);
draw p withcolor framecolor;
draw q withcolor framecolor;
\stopuniqueMPgraphic
\defineoverlay[boxquotebg][\uniqueMPgraphic{box quote}]
\def\boxquote#1{%
  \kern.25em%
  \inframed[frame=off,background=boxquotebg,offset=0pt,loffset=.25em,roffset=.25em]{#1}%
  \kern.25em%
}

\setupexternalfigures[directory={./figures}]

\startMPinclusions
def fancybox(expr u) =
numeric w, h;
w := OverlayWidth; h := OverlayHeight;
color framecolor; framecolor := white randomized (.7, .7, .7);
drawoptions(withpen pencircle scaled 1pt withcolor framecolor);
path p, q;
% 左上角
p := ((0, h) -- (2u, h));
q := ((0, h) -- (0, h - u));
draw p; draw q;
% 左下角
p := ((0, 0) -- (2u, 0));
q := ((0, 0) -- (0, u));
draw p; draw q;
% 右上角
p := ((w - 2u, h) -- (w, h));
q := ((w, h) -- (w, h - u));
draw p; draw q;

% 右下角
p := ((w - 2u, 0) -- (w, 0));
q := ((w, 0) -- (w, u));
draw p; draw q;
enddef;
\stopMPinclusions
\startuseMPgraphic{strange box}
fancybox(.5BodyFontSize);
\stopuseMPgraphic
\defineoverlay[strangeboxbg][\uniqueMPgraphic{strange box}]
\defineframedtext
  [strangebox]
  [frame=off,background=strangeboxbg,
    offset=0pt,loffset=.5em,roffset=.5em,before={\blank},after={\blank[.95em]}]

\setuptype[color=darkred]
\setuptyping
  [before={\startstrangebox[width=\textwidth]},
   after={\stopstrangebox},escape=yes, style=\ttx]


% ---------- example ----------------------
\definebuffer[example]
\definefloat[Example][Examples]

\newdimen\LeftExampleWidth
\newdimen\RightResultWidth
\def\defineLeftExampleWidth#1{%
  \LeftExampleWidth=\textwidth
  \RightResultWidth=\wd#1
  \ifdim\RightResultWidth>0pt
    \advance\LeftExampleWidth by -\RightResultWidth
    \advance\LeftExampleWidth by -1em
  \fi
}
\def\example[#1][#2]#3#4{%
  \setbox\scratchbox\hbox{#4}%
  \defineLeftExampleWidth{\scratchbox}%
  \placeExample[force][#2]{#3}{%
    \hbox to \textwidth{%
      \hbox{\typeexample[#1,
                        before={\startstrangebox[width=\LeftExampleWidth]},
                        after={\stopstrangebox}]}\hss\unhbox\scratchbox}%
  }%
}

\definestartstop
    [MetapostSnippetName]
    [color=darkblue,
     style=boldface]
\definestartstop
    [MetapostSnippetNamePrimitive]
    [color=darkgreen,
      style=boldface]

%%%%% timestamp
\startuniqueMPgraphic{Timestamp}
picture p; p := textext("\currentdate[month, day, {, }, year]");
draw p rotated -45 withcolor darkblue;
\stopuniqueMPgraphic
\definelayer[timestamp][width=\paperwidth, height=\paperheight]
\setlayer[timestamp]
         [preset=righttop, hoffset=5mm, voffset=5mm]
         {\uniqueMPgraphic{Timestamp}}
